<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/

require_once(dirname(__FILE__) . '/app.php');

$id = abs(intval($_GET['id']));
if (!$id || !$team = Table::FetchForce('team', $id) ) {
	redirect( WEB_ROOT . '/team/index.php');
}


if($login_user_id){
   $check_collect = Table::Fetch('collect', $id,'team_id');
		
	if($check_collect['user_id'] == $login_user_id){
		$is_collect = 1;
	}
}
$act = $_GET['act'];
if($act == 'collect'){
	if(!$login_user_id){
        redirect( WEB_ROOT . '/account/login.php');
	}else{
	    $check_collect = Table::Fetch('collect', $id,'team_id');
		
		if($is_collect != 1){
			$collect['team_id'] = $team['id'];
			$collect['user_id'] = $login_user_id;
			$collect['title'] = $team['title'];
			$collect['time'] = time();
			$collect['team_price'] = $team['team_price'];
			$collect['image'] = $team['image'];
			
			$insert = array('team_id','user_id','title','time','team_price','image');
			
			$table = new Table('collect', $collect);
			if ( $table->insert($insert) ){
			   redirect( WEB_ROOT . "/team.php?id={$id}");
			}
		}
	}
}
if($act == 'delcollect'){
   if(Table::Delete('collect', $check_collect['id'])){
      redirect( WEB_ROOT . "/team.php?id={$id}");
   }
}




/* refer */
if ($_rid = strval($_GET['r'])) { 
	$_rid = udecode($_rid);
	$_user = Table::Fetch('user',$_rid,'email');
	if($_user) cookieset('_rid', abs(intval($_user['id'])));
	redirect( WEB_ROOT . "/team.php?id={$id}");
}
$teamcity = Table::Fetch('category', $team['city_id']);
$city = $teamcity ? $teamcity : $city;
$city = $city ? $city : array('id'=>0, 'name'=>'全部');

$pagetitle = $team['title'];

$discount_price = $team['market_price'] - $team['team_price'];
$discount_rate = team_discount($team);

$left = array();
$now = time();

if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}

$diff_time = $left_time = $team['end_time']-$now;
if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
	$diff_time = $left_time = $team['begin_time']-$now;
}

$left_day = floor($diff_time/86400);
$left_time = $left_time % 86400;
$left_hour = floor($left_time/3600);
$left_time = $left_time % 3600;
$left_minute = floor($left_time/60);
$left_time = $left_time % 60;

/* progress bar size */
$bar_size = ceil(190*($team['now_number']/$team['min_number']));
$bar_offset = ceil(5*($team['now_number']/$team['min_number']));
$partner = Table::Fetch('partner', $team['partner_id']);
$team['state'] = team_state($team);

/* your order */
if ($login_user_id && 0==$team['close_time']) {
	$order = DB::LimitQuery('order', array(
				'condition' => array(
					'team_id' => $id,
					'user_id' => $login_user_id,
					'state' => 'unpay',
					),
				'one' => true,
				));
}
/* end order */

/*附近的团购*/
$condition_fj = array( 
		'team_type' => 'normal',
		"begin_time < '{$now}'",
		"end_time > '{$now}'",
		"id in ({$team['fj_team_id']})",     
		);
	
$fjteams = DB::LimitQuery('team', array(
			'condition' => $condition_fj,
			'order' => 'ORDER BY  sort_order  DESC, id DESC',
));


/*买家点评列表*/
$com_list = array();
$comm_list = array();
$comment_list = DB::LimitQuery('order', array(
	'condition' => array(
		'team_id' => $id,
		 'state' => 'pay',
		'comment_display' => 'Y',
		"comment_content != 'Null'",
	),
	'order' => 'ORDER BY id DESC',
	'size'=>30,
));



function get_com_user($id){
   $res = DB::LimitQuery('user',array(
     'condition'=>array(
	     'id'=>$id,
		 ),'one'=>true));
		 return $res;
}


foreach($comment_list as $val){
   if($val['rating_overallrtg'] > 0){
       $comm_list[$val['id']]['id'] = $val['id'];
       $comm_list[$val['id']]['comment_time'] = $val['comment_time'];
       $comm_list[$val['id']]['comment_content'] = $val['comment_content'];
	   $comm_list[$val['id']]['comment_grade'] = $val['comment_grade'];
	   $comm_list[$val['id']]['rating_overallrtg'] = $val['rating_overallrtg'];
	   $comm_list[$val['id']]['reply_time'] = $val['reply_time'];
	   $comm_list[$val['id']]['business_reply'] = $val['business_reply'];
	   $res = get_com_user($val['user_id']);
	   $comm_list[$val['id']][user]=$res;
	   $comm_list[$val['id']]['username'] =  '***'.substr($res['username'],1);
	}
}
foreach($comment_list as $val){
   $com_list[$val['comment_grade']][] = $val['comment_grade'];
   $com_list['rating_overallrtg'][] = $val['rating_overallrtg'];
}

print_r($com_list['rating_overallrtg']);
$verygood = count($com_list[$val['5']]);
$good = count($com_list[$val['4']]);
$in = count($com_list[$val['3']]);
$poor = count($com_list[$val['2']]);
$verypoor = count($com_list[$val['1']]);

$allrtg = $com_list;
$allrtg_num = implode(',', $allrtg);
$allrtg_sum = array($allrtg_num);
$totle = $verygood + $good + $in + $poor + $verypoor;

if($totle == '0'){
   $totle = '1';
   $verygood = '0';
   $good = '0';
   $in = '0';
   $poor = '0';
   $verypoor = '0';
}

//$b_allrtg = round($allrtg_sum/$totle,1);
$b_verygood = round(($verygood/$totle)*100);
$b_good = round(($good/$totle)*100);
$b_in = round(($in/$totle)*100);
$b_poor = round(($poor/$totle)*100);
$b_verypoor = round(($verypoor/$totle)*100);
$fh = '%';
if(($b_verygood + $b_good + $b_in + $b_poor + $b_verypoor) > 100){
    $b_poor = ($b_poor + 100 - ($b_verygood + $b_good + $b_in + $b_poor + $b_verypoor));
	$b_verypoor = ($b_verypoor + 100 - ($b_verygood + $b_good + $b_in + $b_poor + $b_verypoor));	
}

$condition = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'team_id' => $id,
);
$count = Table::Count('order', $condition);


/*kxx team_type */
if ($team['team_type'] == 'seconds') {
	die(include template('team_view'));
}
if ($team['team_type'] == 'goods') {
	die(include template('team_view'));
}
/*xxk*/
$ll = $partner['longlat'];
if ($ll) list($longi,$lati) = preg_split('/[,\s]+/',$ll,-1,PREG_SPLIT_NO_EMPTY);

/*seo*/
$seo_title = $team['seo_title'];
$seo_keyword = $team['seo_keyword'];
$seo_description = $team['seo_description'];
if($seo_title) $pagetitle = $seo_title;
/*end*/

$condition_mm = array(
	'partner_id' => $team['partner_id'],
	"id != {$team['id']}",
	'team_type' => 'normal',
	"begin_time < '{$now}'",
	"end_time > '{$now}'",
);
$count_mm = Table::Count('team', $condition_mm);

list($pagesize, $offset, $pagestring) = pagestring($count, 3);

$team_mm = DB::LimitQuery('team', array(
	'condition' => $condition_mm,
	//'order' => 'ORDER BY comment_time DESC, id DESC',
	//'size' => $pagesize,
	//'offset' => $offset,
));

include template('team_view');
?>
